package socketdemo2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/*
 * 服务端
 * 1.实现向所有客户端信息转发
 * */
public class Server {

    private ServerSocket serverSocket;
    private List<PrintWriter> allOut = new ArrayList<>();

    public Server() {
        try {
            serverSocket = new ServerSocket(8088);
            System.out.println("服务端已开启");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 作为程序的开始方法
     */
    private void start() {
        try {
            while (true) {
                Socket socket = serverSocket.accept();

                Runnable r = new ClientHuandler(socket);
                Thread t = new Thread(r);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server s = new Server();
        s.start();
    }

    /**
     * 老师还没讲为什么要使用内部类
     */
    public class ClientHuandler implements Runnable {

        //因为run方法并不能接收参数 所以使全局变量socket，并且使用构造器进行初始化
        private Socket socket;
        private String host;

        public ClientHuandler(Socket socket) {
            this.socket = socket;
            host = socket.getInetAddress().getHostAddress();
        }

        @Override
        public void run() {
            PrintWriter pw = null;
            try {
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw, true);
                synchronized (allOut) {
                    allOut.add(pw);
                    sendMessage(host + "上线啦!,当前用户在线" + allOut.size());
                }
                String line;
                while ((line = br.readLine()) != null) {
                    sendMessage(host + ":" + line);
                }

            } catch (IOException e) {

                e.printStackTrace();
            } finally {
                synchronized (allOut) {
                    allOut.remove(pw);
                    sendMessage(host + "下线啦!,当前用户在线" + allOut.size());
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        private void sendMessage(String line) {
            //当集合在遍历时，集合是不可以进行增删操作的
            synchronized (allOut) {
                System.out.println(line);
                for (PrintWriter p : allOut
                ) {
                    p.println(line);
                }
            }
        }
    }
}
